<template>
  <div style="height: 600px">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="标题" prop="title">
        <el-input v-model="ruleForm.title"></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <el-input type="textarea" v-model="ruleForm.content"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">发布</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {IP} from "@/config";
import axios from "axios";
import moment from 'moment'
export default {
  data() {
    return {
      ruleForm: {
        title: '',
        content: '',
        time: '',
      },
      rules: {
        title: [
          { required: true, message: '请输入公告标题', trigger: 'blur' },
          { min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入公告内容', trigger: 'blur' },
          { min: 2, max: 99, message: '长度在 2 到 99 个字符', trigger: 'blur' }
        ],
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let currTime = new Date();
          this.ruleForm.time = moment(currTime).format('YYYY-MM-DD HH:mm:ss')
          console.log(this.ruleForm)
          let url = IP + "notice/release"
          axios.post(url,this.ruleForm).then(res=>{
            console.log(res)
            if(res.data.code === 0){
              this.$message({
                type: 'success',
                message: res.data.data.status,
              });
            }else {
              this.$message.error(res.data.data.status);
            }
          }).catch(error=>{
            console.log(error)
          })
          this.ruleForm={
            title: '',
            content: '',
            time: '',
          }
        } else {
          this.$message.error("格式错误！");
          return false;
        }
      });
    },
  }
}
</script>

<style scoped>

</style>